import store from '../store/index'


const db = wx.cloud.database()
const _ = db.command
const $ = db.command.aggregate
const app = getApp()


const getTeam = async(params)=>{
  var teamID = params.teamID
  return new Promise((reslove,reject)=>{
    db.collection('team').doc(teamID).get()
    .then(res=>{
      reslove(res)
    }).catch(err=>{
      reject(err)
    })
  })
}

const getTeamMates = async(params)=>{
  var teamID = params.teamID
  return new Promise((reslove,reject)=>{
    db.collection('team').doc(teamID)
    .field({
      mates:true
    }).get()    
    .then(res=>{
      reslove(res)
    }).catch(err=>{
      reject(err)
    })
  })
}

const publish = async(params)=>{
  var avatarUrl = store.data.user.avatarUrl
  var nickName = store.data.user.nickName
  console.log( params.createTime)
  return new Promise((resolve,reject)=>{
    wx.cloud.callFunction({
      name:'team',
      data:{action:'publish',data:{...params,avatarUrl,nickName}}
    }).then(res=>{
      resolve(res)
    }).catch(err=>{
      reject(err)
    })
  })
 
}


const edit = async(params)=>{
  var id = params.id
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'team',
      data:{
        action:'edit',data:{...params.team},id
      }
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    })
  })
}


const del = async(params)=>{
  var {id} = params
  console.log(id)
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'team',
      data:{action:'del',id}
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    })
  })
}


const list = async(params)=>{
  var openid = store.data.openid
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'team',
      data:{action:'list',...params,openid}
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    })
  })
}


const quit = async(params)=>{
  var {teamID,inc} = params
  console.log(teamID)
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'team',
      data:{action:'quit',teamID:teamID,inc:inc}
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    }) 
  })
}


const join = async(params)=>{
  var avatarUrl = store.data.user.avatarUrl
  var nickName = store.data.user.nickName
  var openid = store.data.openid


  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'team',
      data:{action:'join',data:{...params,avatarUrl,nickName,openid}}
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    }) 
  })
}
const apply = async(params)=>{
  console.log(params)
  var avatarUrl = store.data.user.avatarUrl
  var nickName = store.data.user.nickName
  var openid = store.data.openid
  var initiator = 2
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'team',
      data:{action:'apply',data:{...params,avatarUrl,nickName,openid,initiator}}
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    }) 
  })
}

const accept = async(params)=>{  
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'team',
      data:{action:'accept',data:{...params}}
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    }) 
  })
}






export default {
  publish,
  edit,
  del,
  join,
  list,
  quit,
  apply,
  accept,
  getTeamMates,
  getTeam,
}